#include "CButton.h"

#include "..\..\external_libs\SFML\include\SFML\Graphics\RenderTarget.hpp"
#include "..\..\external_libs\SFML\include\SFML\Graphics\RectangleShape.hpp"
#include "..\..\external_libs\SFML\include\SFML\Graphics\Text.hpp"

CButton::CButton(void)
{
	m_szText = NULL;
}

CButton::CButton(const char* szText, int posX, int posY, int width, int height) : CInteractObject()
{
	
	m_szText = NULL;
	ChangeText(szText);
	m_sRect.top = posY;
	m_sRect.left = posX;
	m_sRect.width = width;
	m_sRect.height = height;
}
CButton::~CButton(void)
{
	delete [] m_szText;
	m_szText = NULL;
}

CButton::CButton(const CButton& obj)
{
	if(this != &obj)
	{
		ChangeText(obj.m_szText);
		m_sRect = obj.m_sRect;
		if(obj.CanBeSelected())
			TurnOn();
		else
			TurnOff();

		
	}
}
CButton& CButton::operator=(const CButton& obj)
{
	if(this != &obj)
	{
		ChangeText(obj.m_szText);
		m_sRect = obj.m_sRect;
		if(obj.CanBeSelected())
			TurnOn();
		else
			TurnOff();

		
	}
	return *this;
}



bool CButton::CheckCollision(int posX, int posY)
{
	return m_sRect.contains(posX, posY);
}
bool CButton::CheckCollision(sf::Vector2<int> pos)
{
	return m_sRect.contains(pos);
}

void CButton::ChangeText(const char* szText)
{
	if(m_szText)
	{
		delete [] m_szText;
	}

	int len = strlen(szText) + 1;

	m_szText = new char[len];


	strcpy_s(m_szText, len, szText);
}


void CButton::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
	sf::RectangleShape rectToDraw = sf::RectangleShape(sf::Vector2f((float)m_sRect.width, (float)m_sRect.height));
	rectToDraw.setOrigin((float)m_sRect.left, (float)m_sRect.top);
	if(!m_bSelected)
		rectToDraw.setFillColor(sf::Color(0,0,0,255));
	else
		rectToDraw.setFillColor(sf::Color(255,0,0,255));

	target.draw(rectToDraw);

	sf::Text textToDraw;
	textToDraw.setString(m_szText);
	textToDraw.setColor(sf::Color(0,255,0,255));

	textToDraw.setOrigin((float)m_sRect.left, (float)m_sRect.top);

	target.draw(textToDraw);
}